package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.MenuMapper;
import org.dey.pojo.Menu;
import org.dey.pojo.PageVo;
import org.dey.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    //左侧菜单数据
    @Override
    public List<Menu> zcmenus(int id) {
        //获取一级菜单
        List<Menu> list =  menuMapper.queryyijicaidan(id);
        //获取二级三级菜单
        for (Menu menu : list) {
            List<Menu> list2 =  menuMapper.queryerjicaidan(menu.getId(),id);
            menu.setChildren(list2);
        }
        return list;
    }

    @Override
    public PageVo<Menu> querymenu(Menu menu, int pageNum, int pagesize) {

        //组装条件
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<Menu>();
        //根据名字模糊查询
        if(StringUtils.isNotBlank(menu.getName())) {
            queryWrapper.like("name", menu.getName());
        }

        //组装分页
        PageHelper.startPage(pageNum, pagesize);
        List<Menu> list = this.list(queryWrapper);

        PageInfo<Menu> pageInfo = new PageInfo<Menu>(list);

        PageVo<Menu> pageVo =new PageVo<Menu>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }

    @Override
    public Result insertmenu(Menu menu) {
        Result result =null;

        boolean one = this.save(menu);

        if(one){
            result = new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_MENU_FAIL);
        }
        //前端弹框取值==> res.data.message

        return result;
    }

    @Override
    public Result delmenu(int id) {
        Result result =null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_MENU_FAIL);
        }
        return result;
    }

    @Override
    public Menu querymenubyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updatemenubyid(Menu menu) {
        Result result =null;
        boolean b = this.updateById(menu);
        if(b){
            result = new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_MENU_FAIL);
        }
        return result;
    }

    @Override
    public List<Menu> menusQbXx() {

        List<Menu> list = menuMapper.menusQbXxyiji();

        for (Menu menus : list) {
            List<Menu> list1 = menuMapper.menusQbXxerji(menus.getId());
            menus.setChildren(list1);
        }

        return list;
    }


    @Override
    public int[] menusBfXx(int rid) {

       List<Menu> list = menuMapper.menusid(rid);

        int[] number = new int[list.size()];
       //获取到数据后 去菜单编号 组装成整数数组
        for (int i = 0; i < list.size(); i++) {
            number[i] = list.get(i).getId();
        }

        return number;
    }

    @Override
    public Result shouquan(int roleid, int[] checkedKeys) {


        Result result = null;

        int shouquan = 0;

        //先根据角色编号删除权限表的数据
      int scqx = menuMapper.scqx(roleid);

       for (int checkedKey : checkedKeys) {

            shouquan = menuMapper.shouquan(roleid, checkedKey);
}
        if(shouquan!=0){
            result = new Result(true,"授权成功");
        }else{
            result = new Result(true,"授权失败");
        }

        return result;
    }


}
